package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty((String)(params.get("key")))){
            queryWrapper.and((v)->{
                v.eq("sku_id",params.get("key")).or().like("sku_name",params.get("key"));
            });
        }
        if (!StringUtils.isEmpty((String)(params.get("catelogId"))) && !params.get("catelogId").equals("0")){
            queryWrapper.eq("catalog_id",params.get("catelogId"));
        }
        if (!StringUtils.isEmpty((String)(params.get("brandId"))) && !params.get("brandId").equals("0")){
            queryWrapper.eq("brand_id",params.get("brandId"));
        }
        if (!StringUtils.isEmpty((String) params.get("min")) && !"0".equalsIgnoreCase(params.get("min").toString())){//大于
            queryWrapper.ge("price",params.get("min"));
        }
        if (!StringUtils.isEmpty((String) params.get("max"))&& !"0".equalsIgnoreCase(params.get("min").toString())){//小于
            queryWrapper.le("price",params.get("min"));
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

}